﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace VisualWip.TFS2010Providers
{
    public class QueryItemToImageSourceConverter : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof (ImageSource))
                throw new InvalidOperationException("Target type must be System.Windows.Media.ImageSource.");

            try
            {
                var folder = values[0] as QueryFolderViewModel;
                if (folder != null)
                {
                    if (folder.Name == "My Queries")
                        return
                            Application.Current.TryFindResource("Tfs2010WorkItemWorkProvider_MyQueries") as ImageSource ??
                            DependencyProperty.UnsetValue;

                    if (folder.Name == "Team Queries")
                        return
                            Application.Current.TryFindResource("Tfs2010WorkItemWorkProvider_TeamQueries") as
                            ImageSource ?? DependencyProperty.UnsetValue;

                    var isExpanded = (bool) values[1];

                    if (isExpanded)
                        return
                            Application.Current.TryFindResource("Tfs2010WorkItemWorkProvider_FolderExpanded") as
                            ImageSource ?? DependencyProperty.UnsetValue;

                    return
                        Application.Current.TryFindResource("Tfs2010WorkItemWorkProvider_FolderCollapsed") as
                        ImageSource ?? DependencyProperty.UnsetValue;
                }

                var query = values[0] as QueryDefinitionViewModel;
                if (query != null)
                {
                    if (query.QueryType == QueryType.List.ToString())
                        return
                            Application.Current.TryFindResource("Tfs2010WorkItemWorkProvider_QueryTypeFlatList") as
                            ImageSource ?? DependencyProperty.UnsetValue;

                    if (query.QueryType == QueryType.OneHop.ToString())
                        return
                            Application.Current.TryFindResource("Tfs2010WorkItemWorkProvider_QueryTypeOneHop") as
                            ImageSource ?? DependencyProperty.UnsetValue;

                    if (query.QueryType == QueryType.Tree.ToString())
                        return
                            Application.Current.TryFindResource("Tfs2010WorkItemWorkProvider_QueryTypeTree") as
                            ImageSource ?? DependencyProperty.UnsetValue;
                }
            }
            catch
            {
                //Do nothing
            }
            return DependencyProperty.UnsetValue;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}